package com.ryan.security.handler;

import com.ryan.security.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kaimin on 1/1/2019.
 * time : 10:30
 */

@ControllerAdvice //处理其它controller抛出来的异常
public class ControllerExcetionhandler {



    @ExceptionHandler(UserNotExistException.class) //指定异常处理
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handUserNotException(UserNotExistException ex){
        Map<String,Object> map=new HashMap<>();
        map.put("id",ex.getId());
        map.put("message",ex.getLocalizedMessage());
        return map;
    }
}
